package com.iplant.wms.controller.wpo;

import com.iplant.base.po.ServiceResult;
import com.iplant.base.po.excel.ExcelData;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.annotation.trace.TraceModuleAPI;
import com.iplant.base.utils.excel.ExcelReader;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.WPOService;
import com.iplant.wms.service.po.wpo.WMSPurchaseOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 采购订单
 *
 * @author YouWang·Peng
 * @CreateTime 2022-4-5 11:43:51
 */
@TraceModuleAPI("采购订单")
@Slf4j
@RestController
@RequestMapping("/api/WMSPurchaseOrder")
public class WMSPurchaseOrderController extends BasicController<WMSPurchaseOrder> {


    @Autowired
    WPOService wWPOService;

    public WMSPurchaseOrderController() {
        super(WMSPurchaseOrder.class);
    }



    /**
     * 创建或更新采购单
     */
    @PostMapping("/UpdateList")
    public Object UpdateList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);
            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            int wNotThrown = wParam.containsKey("NotThrown") ? StringUtils.parseInt(wParam.get("NotThrown")) : 0;
            List<WMSPurchaseOrder> wWMSPurchaseOrderList = CloneTool.CloneArray(wParam.get("data"), WMSPurchaseOrder.class);

            if (wWMSPurchaseOrderList == null || wWMSPurchaseOrderList.size() == 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }
            ServiceResult<Integer> wServiceResult = new ServiceResult<>();
            String wMessage = "";
            for (WMSPurchaseOrder wWMSPurchaseOrder : wWMSPurchaseOrderList) {
                if (wWMSPurchaseOrder == null) {
                    return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                }
                wWMSPurchaseOrder.setUserInfo(wLoginUser);
                if (StringUtils.isEmpty(wWMSPurchaseOrder.Code)) {
                    wWMSPurchaseOrder.Description = RetCode.SERVER_RST_ERROR_OUT + "Code 不能为空";
                    wMessage += StringUtils.Format("\n Code:{0}  Error:{1}", wWMSPurchaseOrder.Code, wWMSPurchaseOrder.Description);

                    continue;
                }
                wServiceResult = wWPOService.WMS_UpdatePurchaseOrder(wLoginUser,
                        wWMSPurchaseOrder);

                if (StringUtils.isNotEmpty(wServiceResult.FaultCode)) {
                    wWMSPurchaseOrder.Description = wServiceResult.FaultCode;

                    wMessage += StringUtils.Format("\n Code:{0}  Error:{1}", wWMSPurchaseOrder.Code, wWMSPurchaseOrder.Description);

                }
            }
            if (StringUtils.isEmpty(wMessage) || wNotThrown == 1) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, wMessage, wWMSPurchaseOrderList, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wMessage);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }




    /**
     * 导入采购订单
     */
    @PostMapping("/Import")
    public Object Import(HttpServletRequest request, @RequestParam("file") MultipartFile[] files) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            if (files.length == 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, "提示：没有要上传的Excel文件！");
            }

            ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>();
            ServiceResult<ExcelData> wExcelData = null;
            String wOriginalFileName = null;
            for (MultipartFile wMultipartFile : files) {
                wOriginalFileName = wMultipartFile.getOriginalFilename();

                if (wOriginalFileName.contains("xlsx")) {
                    wExcelData = ExcelReader.getInstance().readMultiSheetExcel(wMultipartFile.getInputStream(),
                            wOriginalFileName, "xlsx", 1000000);
                } else if (wOriginalFileName.contains("xls")) {
                    wExcelData = ExcelReader.getInstance().readMultiSheetExcel(wMultipartFile.getInputStream(),
                            wOriginalFileName, "xls", 1000000);
                }

                if (StringUtils.isNotEmpty(wExcelData.FaultCode)) {
                    wResult = GetResult(RetCode.SERVER_CODE_ERR, wExcelData.FaultCode);
                    return wResult;
                }

                wServiceResult = wWPOService.WMS_ImportPurchaseOrder(wLoginUser, wExcelData.Result);

                if (!StringUtils.isEmpty(wServiceResult.FaultCode)) {
                    break;
                }
            }

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "导入成功!", null, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }






}
